<?php

declare(strict_types=1);
/**
 * This file is part of MineAdmin.
 *
 * @link     https://www.mineadmin.com
 * @document https://doc.mineadmin.com
 * @contact  root@imoi.cn
 * @license  https://github.com/mineadmin/MineAdmin/blob/master/LICENSE
 */

namespace App\Http\Api\Controller\V1;

use App\Http\Api\Middleware\TokenMiddleware;
use App\Http\Api\Request\V1\UserRequest;
use App\Http\Common\Controller\AbstractController;
use App\Http\Common\Result;
use App\Http\CurrentUser;
use App\Service\PassportService;
use Hyperf\Swagger\Annotation\HyperfServer;
use Hyperf\Swagger\Annotation\Post;
use App\Model\Enums\User\Type;
use Hyperf\HttpServer\Annotation\Middleware;
use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\Swagger\Annotation\Get;

#[HyperfServer(name: 'http')]
final class UserController extends AbstractController
{
    public function __construct(
        private readonly PassportService $passportService,
        private readonly CurrentUser $user
    ) {}

    #[Post(
        path: '/api/v1/login',
        operationId: 'ApiV1Login',
        summary: '用户登录',
        tags: ['api'],
    )]
    public function login(UserRequest $request): Result
    {
        try {
            return $this->success(
                $this->passportService->login(
                    $request->input('username'),
                    $request->input('password'),
                    Type::USER
                )
            );
        } catch (\Throwable $th) {
            return $this->error('账号或者密码错误');
        }
    }

    #[Post(
        path: '/api/v1/register',
        operationId: 'ApiV1register',
        summary: '用户注册',
        tags: ['api'],
    )]
    public function register(UserRequest $request): Result
    {
        $this->passportService->register($request->validated());
        return $this->success();
    }

    #[Get(
        path: '/api/v1/userinfo',
        operationId: 'ApiV1Userinfo',
        summary: '用户信息',
        tags: ['api'],
    )]
    #[Middleware(TokenMiddleware::class)]
    public function info(): Result
    {
        return $this->success(
            $this->user->user()
        );
    }

    #[Get(
        path: '/api/v1/user/course/{course_id:\d+}',
        operationId: 'ApiV1Userinfo',
        summary: '用户信息',
        tags: ['api'],
    )]

    #[Middleware(TokenMiddleware::class)]
    public function course($course_id): Result
    {
        $user = $this->user->user();

        $user->load([
            'courses' =>
            fn($q) => $q->whereCourseId($course_id)->with(['tasks' => function ($q) {
                $q->with(['progress' => function ($q) {
                    $q->whereUserId($this->user->id());
                }])->select('id', 'course_id', 'taskable_type', 'taskable_id');
            }])->limit(1)
        ]);
        if ($user->courses->isEmpty()) {
            return $this->error('未加入该课程');
        }

        return $this->success(
            $user->courses->first()->setVisible(['title', 'tasks'])
        );
    }

    #[Post(
        path: '/api/v1/user/course/{course_id:\d+}/join',
        operationId: 'ApiV1Userinfo',
        summary: '用户信息',
        tags: ['api'],
    )]
    #[Middleware(TokenMiddleware::class)]
    public function joinCourse($course_id): Result
    {
        $user = $this->user->user();
        $user->courses()->sync([$course_id]);
        return $this->success();
    }
}
